/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.User;
import Beans.WatchList;
import Mail.Mail;
import dao.WatchListDao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**

 @author jtanguay
 */
@ManagedBean(name = "watchListManager")
@RequestScoped
public class WatchListManager implements Serializable {

  //Attributs
  @ManagedProperty(value = "#{userManager}")
  private UserManager umanager;
  private WatchList watchList;

  //Getter et setter
  public UserManager getUmanager() {
    return umanager;
  }

  public void setUmanager(UserManager umanager) {
    this.umanager = umanager;
  }

  public WatchListManager() {
  }

  public WatchList getWatchList() {
    return watchList;
  }

  public void setWatchList(WatchList watchList) {
    this.watchList = watchList;
  }

  //Recuperer les favori pour un user
  public List<User> getAllWatchListForUser(int idUser) {
    List<WatchList> listFavori = WatchListDao.getWatchListUser(idUser);
    List<User> listUser = new ArrayList<User>();

    //Recuperer le profil de chaque user dans la liste
    for (WatchList wl : listFavori) {
      User u = umanager.getFavoriteUser(wl.getIdWatchedUser());
      listUser.add(u);
    }
    return listUser;
  }

  //Inserer un favori
  public String getWatchedListForInsert(int idUser, int idWUser, String email, Boolean isWarned, String username) {
    //Recuperation de la liste de favori
    List<WatchList> list = WatchListDao.getWatchListUser(idUser);
    int returnValue;
    //Comportement si la liste est vide
    if (list.isEmpty()) {

      returnValue = WatchListDao.insertInWatchList(idUser, idWUser, true);

      if (returnValue > 0) {

        if (isWarned) {
          Mail.envoyerMail("tpjava3@gmail.com", "Favorite", "You have been added to favorites by " + username);
        }
        FacesMessage msg = new FacesMessage("Add to Favorite successfully attempted.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return "gallery";
      }
    } else {
      for (WatchList wl : list) {

        if ((wl.getIdWatchedUser() == idWUser) && wl.isIsOnWhiteList()) {
          FacesMessage msg = new FacesMessage("This member already in your favorite.");
          FacesContext.getCurrentInstance().addMessage(null, msg);

          return "gallery";
        } else if ((wl.getIdWatchedUser() == idWUser) && !wl.isIsOnWhiteList()) {
          FacesMessage msg = new FacesMessage("You have blocked this member.");
          FacesContext.getCurrentInstance().addMessage(null, msg);

          return "gallery";
        }
      }
      if (isWarned) {
        Mail.envoyerMail("tpjava3@gmail.com", "Favorite", "You have been added to favorites by " + username);
      }
      returnValue = WatchListDao.insertInWatchList(idUser, idWUser, true);

      if (returnValue > 0) {
        FacesMessage msg = new FacesMessage("Add to Favorite successfully attempted.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
      }
    }
    return "gallery";
  }

  //Suppression d'un favori
  public String deleteaWatchList(int idUser, int idWUser, String email, Boolean isWarned, String username) {
    int returnValue = WatchListDao.deleteInWatchList(idUser, idWUser);

    if (returnValue > 0) {
      FacesMessage msg = new FacesMessage("Deleting favorite has succeeded.");
      FacesContext.getCurrentInstance().addMessage(null, msg);

      if (isWarned) {
        Mail.envoyerMail("tpjava3@gmail.com", "Favorite", "you have been deleting favorites of" + username);
      }
    }
    return "favorite";
  }
}
